#include <cstdio>

int main( )
{
    int i, n, m, a, b;
    bool f[ 8 ], t;
    scanf("%d", &n);
    while ( n-- )
    {
        scanf("%d", &m);
        for ( i = 1; i <= 7; i++ )
            f[ i ] = false;
        f[ 2 ] = true;
        while ( m-- )
        {
            scanf("%d%d", &a, &b);
            t = f[ a ];
            f[ a ] = f[ b ];
            f[ b ] = t;
        }
        for ( i = 1; i <= 7; i++ )
            if ( f[ i ] )
                break;
        printf("%d\n", i);
    }
    return 0;
}
